//
//  ProductCell.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/2.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "ProductCell.h"
@interface ProductCell()

@property(nonatomic,strong)UIView * underLine;
@end
@implementation ProductCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.cellHeight = 100;
        self.productImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.productImageView];
        self.productImageView.image = [UIImage imageNamed:@"localDetailIMage_1"];
        
        
        self.productNameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:self.productNameLabel];
        self.productNameLabel.numberOfLines = 3;
        self.productNameLabel.font = [UIFont systemFontOfSize:12];
        self.productNameLabel.text = @"地方环境的规划订货表示我担任公司和丢给";
        
        self.productPriceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:self.productPriceLabel];
        self.productPriceLabel.font = [UIFont systemFontOfSize:12];
        self.productPriceLabel.textAlignment = NSTextAlignmentRight;
        self.productPriceLabel.text = @"¥23000.00";

        
        self.withdrawLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:self.withdrawLabel];
        self.withdrawLabel.font = [UIFont systemFontOfSize:12];
        self.withdrawLabel.textColor = RGBColor(238, 155, 42);
        self.withdrawLabel.text = @"可返现 ¥500.00";
        
        
        
        self.shopNameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:self.shopNameLabel];
        self.shopNameLabel.font = [UIFont systemFontOfSize:12];
        self.shopNameLabel.text = @"绿稀野生专卖店";
        
        self.specLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.specLabel.font = [UIFont systemFontOfSize:12];
//        [self.contentView addSubview:self.specLabel];
        self.specLabel.text = @"大小:180KG";
    }
    return self;
}

-(void)setBackgroundImage:(UIImage *)backgroundImage{
    self.backgroundImageView.image = backgroundImage;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    NSLog(@"1111");
    [self setBackgroundImageViewHeight:self.cellHeight];

    
    self.productImageView.size = CGSizeMake(self.cellHeight-2*globalMargin, self.cellHeight-2*globalMargin);
    self.productImageView.x = 2*globalMargin;
    self.productImageView.y = globalMargin;
    
    
    self.productNameLabel.size = [self.productNameLabel.text sizeInAre:CGSizeMake((self.backgroundImageView.width-self.productImageView.width-2*globalMargin)*0.5, MAXFLOAT) font:self.productNameLabel.font];
    self.productNameLabel.y = self.productImageView.y;
    self.productNameLabel.x = self.productImageView.right+globalMargin;
    
    self.productPriceLabel.size = [self.productPriceLabel.text sizeInAre:CGSizeMake((self.backgroundImageView.width-self.productImageView.width-2*globalMargin)*0.5, MAXFLOAT) font:self.productPriceLabel.font];
    self.productPriceLabel.y = self.productImageView.y;
    self.productPriceLabel.x = ScreenWidth-self.productPriceLabel.width - 2*globalMargin;
    
    self.withdrawLabel.size = [self.withdrawLabel.text sizeInAre:CGSizeMake((self.backgroundImageView.width-self.productImageView.width-2*globalMargin)*0.5, MAXFLOAT) font:self.withdrawLabel.font];
    self.withdrawLabel.y = self.productPriceLabel.botton;
    self.withdrawLabel.x = ScreenWidth-self.withdrawLabel.width - 2*globalMargin;
    
    self.specLabel.size = [self.specLabel.text sizeInAre:CGSizeMake((self.backgroundImageView.width-self.productImageView.width-2*globalMargin), MAXFLOAT) font:self.specLabel.font];
    self.specLabel.botton = self.productImageView.botton;
    self.specLabel.x = self.productNameLabel.x;
    
    self.shopNameLabel.size = [self.shopNameLabel.text sizeInAre:CGSizeMake((self.backgroundImageView.width-self.productImageView.width-2*globalMargin), MAXFLOAT) font:self.shopNameLabel.font];
    self.shopNameLabel.botton = self.specLabel.top;
    self.shopNameLabel.x = self.productNameLabel.x;

}

-(void)setProduct:(id)product{
    _product = product;
    Product * p = (Product*)_product;
    [self.productImageView sd_setImageWithURL:[NSURL URLWithString:p.small_pic] placeholderImage:nil];
    
    
    
    self.productPriceLabel.text = p.price;
    self.productNameLabel.text = p.name;
    NSRange startRange = [p.name rangeOfString:@"【"];
    NSRange endRange = [p.name rangeOfString:@"】"];
    NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
    if(range.length!=0){
        NSString *result = [p.name substringWithRange:range];
        self.shopNameLabel.text = result;
    }else{
        self.shopNameLabel.text = @"";
    }
    
    self.withdrawLabel.text = p.cost;
}
@end
